﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LinuxManage.Kit;
using FSM.DotNetSSH;
using System.IO;

namespace LinuxManage.winForm
{
    public partial class EmailDebug : Form
    {
        public SshExec exec;
        public EmailDebug()
        {
            InitializeComponent();
        }

        public SshExec getSSHExec()
        {
            SshExec exec = new SshExec(Config.host, Config.user, Config.passwd);
            exec.Connect();
            return exec;
        }
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (exec == null)
            {
                try
                {
                exec = new SshExec(Config.host, Config.user, Config.passwd);
                    exec.Connect();
                }
                catch (Exception ex)
                {
                    connectStatus.Text = "连接失败";
                    MessageBox.Show(ex.Message);
                    return;
                }
                connectStatus.Text = "连接成功";
                btnConnect.Text = "关闭连接";
            }
            else
            {
                exec.Close();
                connectStatus.Text = "等待连接";
                btnConnect.Text = "开启连接";
            }
           
        }

        private void btnClearPostifxLog_Click(object sender, EventArgs e)
        {
            showDoMsg.Text = "清理postfix日志";
            string commandText = "echo > /var/log/maillog";
            try
            {
                exec = getSSHExec();
                string output = exec.RunCommand(commandText);
                exec.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("清理成功");
        }

        private void btnLookPostfixLog_Click(object sender, EventArgs e)
        {
            showDoMsg.Text = "查看postfix日志";
            string commandText = "cat  /var/log/maillog";
            try
            {
                exec = getSSHExec();
                string output = exec.RunCommand(commandText);
                exec.Close();
                #region
                string path = @"F:\postfixLog.txt";
                StreamWriter sw = new StreamWriter(path, false);
                sw.WriteLine(output);
                sw.Close();//写入
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("执行成功");
        }

        private void btnClearDovecotLog_Click(object sender, EventArgs e)
        {
            showDoMsg.Text = "清理dovecot日志";
            string commandText = "echo > /var/log/dovecot.log";
            try
            {
                exec = getSSHExec();
                string output = exec.RunCommand(commandText);
                exec.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("清理成功");
        }

        private void btnLookDovecotLog_Click(object sender, EventArgs e)
        {
            showDoMsg.Text = "查看dovecot日志";
            string commandText = "cat /var/log/dovecot.log";
            try
            {
                exec = getSSHExec();
                string output = exec.RunCommand(commandText);
                exec.Close();
                #region
                string path = @"F:\dovecotLog.txt";
                StreamWriter sw = new StreamWriter(path, false);
                sw.WriteLine(output);
                sw.Close();//写入
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("执行成功");
        }

        private void EmailDebug_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (exec != null)
            {
                exec.Close();
            }
            MessageBox.Show("关闭成功");
        }
    }
}
